package com.bt.controller;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bt.annotation.RequiresPermissionsDesc;
import com.bt.pojo.DtsBrand;
import com.bt.service.DtsAdminService;
import com.bt.service.DtsBrandService;
import com.bt.service.DtsCategoryService;
import com.bt.util.ResponseUtil;
import com.bt.vo.AdminVo;
import com.bt.vo.CatVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wbt
 * @version v1.0
 * @project shop_manage
 * @data 2022/9/24 19:33
 **/
@RestController
@CrossOrigin
@RequestMapping("/admin/brand")
public class AdminBrandController {
    @Autowired
    private DtsBrandService dtsBrandServiceImpl;
    @Autowired
    private DtsAdminService dtsAdminServiceImpl;
    @Autowired
    private DtsCategoryService dtsCategoryServiceImpl;
    /**
     * http://localhost:8080/admin/brand/list?page=1&limit=20&id=11&name=22&sort=add_time&order=desc
     * get
     */
    @GetMapping("/list")
    @RequiresPermissions("admin:brand:list")
    @RequiresPermissionsDesc(menu = {"系统管理", "商品管理"}, button = "查询商品品牌")
    public Object list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "20") Integer limit, Integer id, String name, String sort, String order){
        if (page<1){
            page=1;
        }
        if (limit<1){
            limit=20;
        }
        IPage<DtsBrand> Ipage=dtsBrandServiceImpl.findBrandByPage(page,limit,id,name,sort,order);
        Map data=new HashMap<>();
        data.put("tatal",Ipage.getTotal());
        data.put("items",Ipage.getRecords());
        return ResponseUtil.ok(data);
    }
    /**
     * http://localhost:8080/admin/brand/catAndAdmin
     * get
     */
    @GetMapping("/catAndAdmin")
    @RequiresPermissions("admin:brand:catAndAdmin")
    @RequiresPermissionsDesc(menu = {"系统管理", "商品管理"}, button = "查询品牌和管理员列表")
    public Object catAndAdmin(){
        List<AdminVo> adminList=dtsAdminServiceImpl.findDtsAdminVo();
        List<CatVo> catVoList=dtsCategoryServiceImpl.findCatVos();
        Map data=new HashMap();
        data.put("adminList",adminList);
        data.put("categoryList",catVoList);
        return ResponseUtil.ok(data);
    }
    /**
     * http://localhost:8080/admin/brand/update
     * post
     */

    @PostMapping("update")
    @RequiresPermissions("admin:brand:update")
    @RequiresPermissionsDesc(menu = {"系统管理", "商品管理"}, button = "更新商品品牌")
    public Object update(@RequestBody DtsBrand dtsBrand){
        try {
            dtsBrandServiceImpl.updateDtsBrand(dtsBrand);
            return ResponseUtil.ok(dtsBrand);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.fail(500,"更新失败");
        }
    }
    /**
     * http://localhost:8080/admin/brand/create
     * post
     */
    @PostMapping("create")
    @RequiresPermissions("admin:brand:create")
    @RequiresPermissionsDesc(menu = {"系统管理", "商品管理"}, button = "创建商品品牌")
    public Object create(@RequestBody DtsBrand dtsBrand){
        if (StringUtils.isEmpty(dtsBrand.getName())){
            return ResponseUtil.fail(500,"品牌名称不能为空");
        }
        try {
            dtsBrandServiceImpl.insertDtsBrand(dtsBrand);
            return ResponseUtil.ok(dtsBrand);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.fail(500,e.getMessage());
        }
    }
    /**
     *  http://localhost:8083/admin/brand/delete
     *  post
     */
    @PostMapping("/delete")
    @RequiresPermissions("admin:brand:delete")
    @RequiresPermissionsDesc(menu = {"系统管理", "商品管理"}, button = "删除商品品牌")
    public Object delete(@RequestBody DtsBrand dtsBrand){
        if (dtsBrand.getId()==null){
            return ResponseUtil.fail(500,"id不能为空");
        }
        try {
            dtsBrandServiceImpl.deleteDtsBrand(dtsBrand);
            return ResponseUtil.ok(dtsBrand);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseUtil.fail(500,e.getMessage());
        }
    }







}
